﻿
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text;
    using KeRui.BLL;
    using KeRui.Common;
    using System.Collections.Generic;

    /// <summary>
    /// ManagePageHelper 的摘要说明
    /// </summary>
    public class ManagePageHelper
    {

        public ManagePageHelper()
        {

        }

        /// <summary>
        /// 生成栏目的树形下拉格式
        /// </summary>
        /// <param name="siteID">站点ID</param>
        /// <returns>格式化的option栏目列表</returns>
        public string BuildNodeList(int siteID)
        {
            StringBuilder stringBuilder2 = new StringBuilder();
            KeRui.BLL.CmsNode bllCmsNode = new CmsNode();
            List<KeRui.Model.CmsNode> list = bllCmsNode.GetList(PageRequest.GetInt("siteid", -1));
            foreach (KeRui.Model.CmsNode model in list)
            {
                switch (model.Depth)
                {
                    case 0:
                        stringBuilder2.AppendFormat("<option value='{0}'>└首 页</option>", model.NodeID);
                        stringBuilder2.Append(BuildFormatOptions(model.arrChildID));
                        break;
                }
            }
            return stringBuilder2.ToString();
        }

        public string BuildNodeList(int siteID, int nodeid)
        {
            StringBuilder stringBuilder2 = new StringBuilder();
            KeRui.BLL.CmsNode bllCmsNode = new CmsNode();
            List<KeRui.Model.CmsNode> list = bllCmsNode.GetList(PageRequest.GetInt("siteid", -1));
            foreach (KeRui.Model.CmsNode model in list)
            {
                switch (model.Depth)
                {
                    case 0:
                        if (nodeid == model.NodeID)
                        {
                            stringBuilder2.AppendFormat("<option selected='selected'  value='{0}'>└首 页</option>", model.NodeID);
                            stringBuilder2.Append(BuildFormatOptions(model.arrChildID, nodeid));
                        }
                        else
                        {
                            stringBuilder2.AppendFormat("<option value='{0}'>└首 页</option>", model.NodeID);
                            stringBuilder2.Append(BuildFormatOptions(model.arrChildID, nodeid));
                        }
                        break;
                }
            }
            return stringBuilder2.ToString();
        }

        private string BuildFormatOptions(string arraychildIDs, int nodeid)
        {
            StringBuilder stringBuilder = new StringBuilder();
            string[] childnodes = arraychildIDs.Split(',');
            KeRui.BLL.CmsNode bllCmsNode = new CmsNode();
            for (int i = 0; i < childnodes.Length; i++)
            {
                if (i != childnodes.Length - 1 && childnodes[i] != string.Empty)
                {
                    if (childnodes[i] == nodeid.ToString())
                    {
                        stringBuilder.AppendFormat("<option selected='selected' value='{0}'>{2}├ {1}</option>", bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeID, bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeName, BuildEmpty(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).Depth));
                        BuildFormatOptions(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).arrChildID);
                    }
                    else
                    {
                        stringBuilder.AppendFormat("<option value='{0}'>{2}├ {1}</option>", bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeID, bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeName, BuildEmpty(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).Depth));
                        BuildFormatOptions(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).arrChildID);
                    }

                }
                else if (childnodes[i] != string.Empty)
                {
                    if (childnodes[i] == nodeid.ToString())
                    {
                        stringBuilder.AppendFormat("<option selected='selected'  value='{0}'>{2}└ {1}</option>", bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeID, bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeName, BuildEmpty(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).Depth));
                        BuildFormatOptions(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).arrChildID);
                    }
                    else
                    {
                        stringBuilder.AppendFormat("<option value='{0}'>{2}└ {1}</option>", bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeID, bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeName, BuildEmpty(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).Depth));
                        BuildFormatOptions(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).arrChildID);
                    }

                }
            }
            return stringBuilder.ToString();
        }



        private string BuildFormatOptions(string arraychildIDs)
        {
            StringBuilder stringBuilder = new StringBuilder();
            string[] childnodes = arraychildIDs.Split(',');
            KeRui.BLL.CmsNode bllCmsNode = new CmsNode();
            for (int i = 0; i < childnodes.Length; i++)
            {
                if (i != childnodes.Length - 1 && childnodes[i] != string.Empty)
                {
                    stringBuilder.AppendFormat("<option value='{0}'>{2}├ {1}</option>", bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeID, bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeName, BuildEmpty(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).Depth));
                    BuildFormatOptions(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).arrChildID);
                }
                else if (childnodes[i] != string.Empty)
                {
                    stringBuilder.AppendFormat("<option value='{0}'>{2}└ {1}</option>", bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeID, bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).NodeName, BuildEmpty(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).Depth));
                    BuildFormatOptions(bllCmsNode.GetModel(Convert.ToInt32(childnodes[i])).arrChildID);
                }
            }
            return stringBuilder.ToString();
        }

        private static string BuildEmpty(int depth)
        {
            StringBuilder stringBuilder1 = new StringBuilder();
            for (int i = 0; i < depth; i++)
            {
                stringBuilder1.Append("&nbsp;&nbsp;");
            }
            return stringBuilder1.ToString();
        }
    }
